package com.platform.usercenter.controller;

import com.platform.common.core.domain.entity.ImUser;
import com.platform.common.core.domain.entity.SysUser;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.common.security.annotation.RequiresPermissions;
import com.platform.common.security.utils.SecurityUtils;
import com.platform.usercenter.domain.vo.UserVo;
import com.platform.usercenter.mapper.SysUserMapper;
import com.platform.usercenter.service.ImService;
import com.platform.usercenter.utils.HashCodeUtil;
import com.platform.usercenter.utils.ListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/imUser")
public class ImUserController extends BaseController {

    @Resource
    ImService imService;

    @Resource
    SysUserMapper sysUserMapper;

    /**
     * 批量注册用户到IM(前端页面在用)
     */
    @RequiresPermissions("system:user:registIm")
    @PostMapping("/registerBatchUsers")
    public AjaxResult registerBatchUsers(@RequestBody UserVo userVo){
        try {
            if(StringUtils.isEmpty(userVo.getUserids())){
                return AjaxResult.error("参数缺失");
            }
            List<Long> listU=ListUtil.convey(ListUtil.split(userVo.getUserids(),","),Long.class);
            Long[] userids=listU.stream().toArray(Long[]::new);
            imService.registerBatchUsers(userids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("注册用户到Im异常:"+e.getMessage());
        }
        return AjaxResult.success("注册成功");
    }

    /**
     * 批量修改用户(昵称和密码)到IM(前端页面在用))
     */
    @RequiresPermissions("system:user:editIm")
    @PostMapping("/updateBatchUsers")
    public AjaxResult updateBatchUsers(@RequestBody UserVo userVo){
        try {
            if(StringUtils.isEmpty(userVo.getUserids())){
                return AjaxResult.error("参数缺失");
            }
            List<Long> listU=ListUtil.convey(ListUtil.split(userVo.getUserids(),","),Long.class);
            Long[] userids=listU.stream().toArray(Long[]::new);
            imService.updateBatchUsers(userids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改Im用户信息异常:"+e.getMessage());
        }
        return AjaxResult.success("操作成功");
    }

    /**
     * Im客户端（PC,手表,unaipp，小程序）在用
     * @return
     */
    @RequestMapping("/getImPwd")
    public ImUser getImPwd(){
        String userName=SecurityUtils.getUsername();
        logger.info("******userName:{}",userName);
        ImUser user=sysUserMapper.selectSysUserPwdByUserName(userName);
        int impwd= HashCodeUtil.getHashCode(user.getPwd());
        user.setPwd("");
        user.setImPwd(impwd);
        return user;
    }

    /**
     * 批量删除Im用户(前端在用)
     * @return
     */
    @RequiresPermissions("system:user:deleteIm")
    @PostMapping("/deleteBatchUsers")
    public AjaxResult deleteBatchUsers(@RequestBody UserVo userVo){
        if(StringUtils.isEmpty(userVo.getUserids())){
            return AjaxResult.error("参数缺失");
        }
        try {
            List<Long> listU=ListUtil.convey(ListUtil.split(userVo.getUserids(),","),Long.class);
            Long[] userids=listU.stream().toArray(Long[]::new);
            imService.deleteBatchImUser(userids);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }

    /**
     * 全量删除Im用户
     * @return
     */
    @RequestMapping("/deleteImUser")
    public AjaxResult deleteImUser(){

        try {
            imService.deleteImUser();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }


    @RequestMapping("/registerAllUserToIm")
    public AjaxResult registerAllUserToIm(){
        try {
            imService.registerAllUserToIm();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("注册成功");
    }

    @RequestMapping("/updateAllUserToIm")
    public AjaxResult updateAllUserToIm(){
        try {
            imService.updateAllUserToIm();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("注册成功");
    }

    @RequestMapping("/updateUserPasswordToIm")
    public AjaxResult updateUserPasswordToIm(SysUser user){
        if(com.platform.common.core.utils.StringUtils.isEmpty(user.getUserName())){
            return AjaxResult.error("参数缺失");
        }
        try {
            String userName=SecurityUtils.getUsername();
            imService.updateImUserPwdByUserName(user.getUserName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("修改密码成功");
    }

    @RequestMapping("/updateUserPasswordToImByUserName")
    public AjaxResult updateUserPasswordToIm(String username){
        if(com.platform.common.core.utils.StringUtils.isEmpty(username)){
            return AjaxResult.error("参数缺失");
        }
        try {
            imService.updateImUserPwdByUserName(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("修改密码成功");
    }

    @RequestMapping("/updateImUserInfo")
    public AjaxResult updateImUserInfo(String username){
        try {
            imService.updateImUserInfo(username);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("设置失败");
        }
        return AjaxResult.success("设置成功");
    }
}
